package com.cloud.shopping.page.feign.service.impl;

import com.alibaba.fastjson.JSON;
import com.cloud.shopping.goods.feign.CategoryFeign;
import com.cloud.shopping.goods.feign.SpuFeign;
import com.cloud.shopping.goods.model.Category;
import com.cloud.shopping.goods.model.Product;
import com.cloud.shopping.goods.model.Sku;
import com.cloud.shopping.goods.model.Spu;
import com.cloud.shopping.page.feign.service.PageService;
import com.cloud.shopping.seckill.feign.SeckillGoodsFeign;
import com.cloud.shopping.tool.RespResult;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import javax.annotation.Resource;
import java.io.File;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author John.zhang
 * @version Id: PageServiceImpl, v 1.0 2021/1/3 13:17 ZhiYong Exp $
 */
@Service(value = "ShoppingPageServiceImpl")
public class PageServiceImpl implements PageService {

    @Resource
    private CategoryFeign categoryFeign;

    @Resource
    private SeckillGoodsFeign seckillGoodsFeign;

    @Resource
    private SpuFeign spuFeign;

    @Resource
    private TemplateEngine templateEngine;

    @Value("${pagePath}")
    private String pagePath;

    @Override
    public void html(String spuId) throws Exception {
        //1、创建容器对象(上下文对象)
        Context context = new Context();
        //2、设置模板数据
        context.setVariables(loadData(spuId));
        //3、指定文件生成后存储路径
        File file = new File(pagePath, spuId + ".html");
        PrintWriter writer = new PrintWriter(file, "UTF-8");
        //4、执行合成生成
        templateEngine.process("item", context, writer);
    }

    /**
     * 数据加载
     *
     * @param spuId spuId
     * @return Map
     */
    public Map<String, Object> loadData(String spuId) {
        //查询数据
        RespResult<Product> productResult = spuFeign.one(spuId);
        Product product = productResult.getData();
        if (product == null) {
            return null;
        }
        //Map
        Map<String, Object> resultMap = new HashMap<>(16);
        //Spu
        Spu spu = product.getSpu();
        resultMap.put("spu", spu);
        //图片处理
        resultMap.put("images", spu.getImages().split(","));
        //属性列表
        resultMap.put("attrs", JSON.parseObject(spu.getAttributeList(), Map.class));

        //三级分类
        RespResult<Category> one = categoryFeign.one(spu.getCategoryOneId());
        RespResult<Category> two = categoryFeign.one(spu.getCategoryTwoId());
        RespResult<Category> three = categoryFeign.one(spu.getCategoryThreeId());
        resultMap.put("one", one.getData());
        resultMap.put("two", two.getData());
        resultMap.put("three", three.getData());

        //Sku集合
        List<Map<String, Object>> skuList = new ArrayList<>();
        for (Sku sku : product.getSkuas()) {
            Map<String, Object> skuMap = new HashMap<>(16);
            skuMap.put("id", sku.getId());
            skuMap.put("name", sku.getName());
            skuMap.put("price", sku.getPrice());
            skuMap.put("attr", sku.getSkuAttribute());
            //将skuMap添加到skuList中
            skuList.add(skuMap);
        }
        resultMap.put("skuList", skuList);
        return resultMap;
    }


}
